/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.helper;

import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * HTMLUtil provides some static functions to edit HTML text
 *
 * @author Markus Moormann
 */
public class HTMLUtil {

	/**
	 * strips all HTML tags out of the text apart from the given one
	 * 
	 * @param html the text to strip
	 * @param tags the tags to keep
	 * @return the text without HTML tags apart from the given ones
	 */
	public static String stripHtmlTags(String html, String tags) {
		String[] tagsToBeKept = new String[4];
		if (tags.length() > 0)
			tagsToBeKept = tags.split("\\s*,\\s*");

		LinkedList<String> tagsToKeep = new LinkedList<String>();
		for (int i = 0; i < tagsToBeKept.length; i++) {
			if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
				tagsToKeep.add(tagsToBeKept[i]);
		}

		LinkedList<String> toBeRemoved = new LinkedList<String>();
		Matcher tagRegExp = Pattern.compile("<([^>\\s]+)(\\s[^>]+)*>",
				Pattern.CASE_INSENSITIVE).matcher(html);
		LinkedList<String> foundedStrings = new LinkedList<String>();
		while (tagRegExp.find()) {
			foundedStrings.add(tagRegExp.group());
		}

		for (int i = 0; i < foundedStrings.size(); i++) {
			Boolean tagFlag = false;
			if (tagsToKeep != null) {
				for (int j = 0; j < tagsToKeep.size(); j++) {
					String tmpRegExp = "<\\/?" + tagsToKeep.get(j) + "[^<>]*?>";
					String tmpStr = foundedStrings.get(i);
					if (tmpStr.matches(tmpRegExp))
						tagFlag = true;
				}
			}
			if (!tagFlag)
				toBeRemoved.add(foundedStrings.get(i));
		}
		for (int i = 0; i < toBeRemoved.size(); i++) {
			html = html.replaceAll(toBeRemoved.get(i), "");
		}
		return html;
	}
	
	/**
	 * removes the title tag from HTML
	 * 
	 * @param html the text to remove the title tag from
	 * @return the HTML text without the title tag
	 */
	public static String removeTitle(String html)
	{
		String reg = "(?<=title=['|\"])[^'|\"]*?(?=['|\"])";
		return html.replaceAll(reg, "");
	}
	
	/**
	 * removes all links from HTML
	 * 
	 * @param html the text to remove the links from
	 * @return the text without the links
	 */
	public static String removeHref(String html)
	{
		String reg ="(?=<a href).*(?<=</a>)";
		return html.replaceAll(reg, "");
	}

}
